package com.example.sxmall.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.sxmall.common.dto.PercontentDto;
import com.example.sxmall.dao.mapper.AdminsMapper;
import com.example.sxmall.dao.pojo.Admins;
import com.example.sxmall.dao.pojo.Percontent;
import com.example.sxmall.dao.mapper.PercontentMapper;
import com.example.sxmall.service.AdminsService;
import com.example.sxmall.service.PercontentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhang
 * @since 2022-03-22
 */
@Service
public class PercontentServiceImpl extends ServiceImpl<PercontentMapper, Percontent> implements PercontentService {

    @Autowired
    AdminsService adminsService;
    @Autowired
    AdminsMapper adminsMapper;

    @Override
    public List<PercontentDto> getCurrentAdminsNav() {
        //获取当前用户信息
        String account = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Admins admins = adminsService.getByAdminsName(account);

        List<Integer> percontentId = adminsMapper.getNavPercontentsId(admins.getAid());
        List<Percontent> percontents = this.listByIds(percontentId);
        //转树桩结构
        List<Percontent> percTree = buildTreePerc(percontents);

        //实体转dto
        return convert(percTree);
    }

    @Override
    public List<Percontent> tree() {
        //获取所有权限信息
        List<Percontent> percontents = this.list(new QueryWrapper<Percontent>());

        //转成树桩结构

        return buildTreePerc(percontents);
    }

    private List<PercontentDto> convert(List<Percontent> percTree) {
        List<PercontentDto> pctDto = new ArrayList<>();
        percTree.forEach(pcTree -> {
            PercontentDto dto = new PercontentDto();
            dto.setId(pcTree.getPcid());
            dto.setName(pcTree.getPcitems());
            dto.setTitle(pcTree.getName());
            //dto.setComponent(pcTree.get);
            dto.setPath(pcTree.getPcurl());

            if (pcTree.getChildren().size() > 0){
                //子节点调用当前方法进行再次转换
                dto.setChildren(convert(pcTree.getChildren()));
            }

            pctDto.add(dto);
        });

        return pctDto;
    }

    private List<Percontent> buildTreePerc(List<Percontent> percontents) {

        List<Percontent> finalPct = new ArrayList<>();

        //先各自寻找到各自的孩子
        for (Percontent pcts: percontents) {
            for (Percontent pct: percontents){
                if (pcts.getPcid() == pct.getParentid()){
                    pcts.getChildren().add(pct);
                }
            }
            //提取出父节点

            if (pcts.getParentid() == 0){
                finalPct.add(pcts);
            }
        }
        System.out.println(JSONUtil.toJsonStr(finalPct));

        return finalPct;
    }

}
